#       FrontendFeed_test.py
#       
#       Copyright 2008 Joshua Scotton <josh@joshuascotton.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

# Some code in setUp borrowed from Matt Layman's MusicLibrary_test.py

__licence__ = "GPLv2"
__copyright__ = "2008, Joshua Scotton"
__author__ = "Joshua Scotton <josh@joshuascotton.com>"

import unittest
import sys
import os

from pysqlite2 import dbapi2 as sqlite

sys.path += [os.path.abspath('..')]

from frontend.medialibrary.feeds import Feed, Entry

class FrontendFeedTest(unittest.TestCase):

    def setUp(self):
        """Sets up everything for the test"""
        self.feed_db = os.path.abspath('.' + '/data/' + self.__str__() + '.db')
        if os.path.exists(self.feed_db):
            os.remove(self.feed_db) # Start fresh to avoid table creation error
        db_conn = sqlite.connect(self.feed_db)
        db_cursor = db_conn.cursor()
        db_cursor.execute("""CREATE TABLE feed(
                                url TEXT, 
                                title TEXT, 
                                subtitle TEXT, 
                                description TEXT, 
                                time TIME,
                                date DATE,
                                PRIMARY KEY(url))""")
                                
        db_cursor.execute("""CREATE TABLE entry(
                                feed_url TEXT,
                                title TEXT,
                                description TEXT,
                                isread TEXT,
                                time TIME,
                                date DATE,
                                id TEXT,
                                PRIMARY KEY(id))""")
        db_conn.commit()
        for i in range(10):
            feed_row = ("url"+str(i), 
                        "title"+str(i), 
                        "subtitle"+str(i), 
                        "description"+str(i),
                        "2008-01-01",
                        "12:00:00")
            db_cursor.execute("""INSERT INTO feed(url,title,subtitle,description,date,time) 
                                 VALUES (?,?,?,?,?,?)""", feed_row)
            db_conn.commit()
        for i in range(9):
            if i%2==0:
                read = "NO"
            else:
                read = "YES"
            entry_row = ("url1",
                         "entry"+str(i), 
                         "desc",
                         read,
                         str(i),
                         "2008-01-01",
                        "12:00:00")
            db_cursor.execute("""INSERT INTO entry(feed_url,title,description,isread,id,date,time) 
                                 VALUES (?,?,?,?,?,?,?)""", entry_row)
            db_conn.commit()
        db_conn.close()
        self.feed = Feed("url1", self.feed_db)
        
    def tearDown(self):
        """
        Removes test database file
        """
        if os.path.exists(self.feed_db):
                os.remove(self.feed_db)
        
    def testGetTitle(self):
        """
        testGetTitle: Tests Feed.get_title
        """
        self.assertEqual("title1", self.feed.get_title())
        
    def testGetDescription(self):
        """
        testGetDescription: Tests Feed.get_description()
        """
        self.assertEqual("description1", self.feed.get_description())
        
    def testGetEntries(self):
        """
        testGetEntries: Tests Feed.get_entries()
        """
        self.assertEqual(9, len(self.feed.get_entries()))
        
    def testGetEntry(self):
        """
        testGetEntry: Tests Feed.get_entry()
        """
        self.assertEqual("entry3", self.feed.get_entry("3").get_title())
        
    def testGetNumberOfUnread(self):
        """
        testGetNumberOfUnread: Tests Feed.get_number_of_unread()
        """
        self.assertEqual(5, self.feed.get_number_of_unread())
        
    def testGetSubtitle(self):
        """
        testGetSubtitle: Tests Feed.get_subtitle()
        """
        self.assertEqual("subtitle1", self.feed.get_subtitle())
        
    def testGetFeedUrl(self):
        """
        testGetFeedUrl: Tests Feed.get_feed_url()
        """
        self.assertEqual("url1", self.feed.get_feed_url())
        
    def testGetTime(self):
        """
        testGetTime: Tests Feed.get_time()
        """
        self.assertEqual("12:00:00", self.feed.get_time())
        
    def testGetDate(self):
        """
        testGetDate: Tests Feed.get_date()
        """
        self.assertEqual("2008-01-01", self.feed.get_date())
